package com.weme.mine.fragment

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.jeremyliao.liveeventbus.LiveEventBus
import com.makeramen.roundedimageview.RoundedImageView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.weme.common.arouter.ARouterHelper
import com.weme.common.base.BaseFragment
import com.weme.common.util.*
import com.weme.common.view.SmoothImageView
import com.weme.mine.R
import com.weme.mine.databinding.FragmentFollwingBinding
import com.weme.mine.viewmode.FollowViewMode
import dev.kratos.api.user_server.v1.FollowListReply
import dev.kratos.api.user_server.v1.Users

class FansFragment : BaseFragment<FragmentFollwingBinding, FollowViewMode>() {
    private val userList = mutableListOf<Users>()
    var pageIndex = 1;
    override fun startOperate() {
        mBinding.rcvList.setVerticalLayoutManager()
        mBinding.rcvList.adapter = histortAdapter
        histortAdapter.isStateViewEnable = true
        histortAdapter.submitList(userList)
        val view = View.inflate(requireContext(), com.weme.common.R.layout.layout_empty_state, null)
        val btRefresh=  view.findViewById<TextView>(com.weme.common.R.id.btRefresh)
        btRefresh.setOnClickListener {
//            UtilsApp.getActivityList().forEach {
//                if(!it.get()?.localClassName.equals("MainActivity")){
//                    it.get()?.finish()
//                }
//            }
         LiveEventBus.get<Any>(LiveConfig.NEW_HOME_INDEX).post(1)

requireActivity().finish()
        }
        view.findViewById<TextView>(com.weme.common.R.id.tv_empty).setText(com.example.resources.R.string.follwers_empty_txt)
        histortAdapter.stateView = view //  histortAdapter.setEmptyViewLayout(requireContext(),)
        histortAdapter.setOnItemClickListener{ _, _, index ->

            ARouterHelper.navigateToUserInfoActivity(requireContext(), userList.get(index).uid)
        }
     LiveEventBus.get<Any>(LiveConfig.NET_WORK_FANS).observe(this){
            if(it is FollowListReply){
                if(pageIndex==1){
                    userList.clear()
                    userList.addAll(it.usersList)
                }else{
                    userList.addAll(it.usersList)
                }
                mBinding.refreshFollow.finishRefresh()
                mBinding.refreshFollow.finishLoadMore()
                histortAdapter.notifyDataSetChanged()
            }

        }
      //  viewModel.getFansListApi(pageIndex)

        mBinding.refreshFollow.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageIndex = 1
                viewModel.getFansListApi(pageIndex)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageIndex++
                viewModel.getFansListApi(pageIndex)
            }

        })
    }

    override fun onResume() {
        super.onResume()
        if(userList.isEmpty()){
            mBinding.refreshFollow.post {
                mBinding.refreshFollow.autoRefresh()
            }
        }else{
            pageIndex = 1
            viewModel.getFansListApi(pageIndex)
        }

    }

    private val histortAdapter = object : BaseQuickAdapter<Users, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: Users?) {
            item?.apply {
                val imCall = holder.getView<ImageView>(R.id.imCall)
                imCall.setOnFastClickListener {
                    JumpUserManager.jumpCallActivity(this)
                }
                val rimHead = holder.getView<RoundedImageView>(R.id.rimHead)
                rimHead.setOnFastClickListener {
                    JumpUserManager.jumpUserInfoActivity(this.uid,this.nickname)
                }
                val country = getCountryBean(item.countryCode)
                country?.let {
                    val imFlag = holder.getView<SmoothImageView>(R.id.imFlag)
                    Glide.with(requireContext()).load(it.countryFlag).into(imFlag)
                }
                rimHead.loadImage(item.headPic)
                val vCircle = holder.getView<View>(R.id.vCircle) //在线状态 0离线 1在线 ,2忙碌
                when (item.onlineStatus) {
                    1 -> {
                        vCircle.visible()
                        vCircle.setBackgroundResource(com.example.resources.R.drawable.online_dot)
                      //  holder.setText(R.id.tvOnline, getString(com.example.resources.R.string.online))
                    }
                    2 -> {
                        vCircle.visible()
                        vCircle.setBackgroundResource(com.example.resources.R.drawable.busy_dot)
                    //    holder.setText(R.id.tvOnline, getString(com.example.resources.R.string.busy))
                    }
                    else -> {
                        vCircle.gone()
                    //    holder.setText(R.id.tvOnline, getString(com.example.resources.R.string.online))
                    }
                }
                holder.setText(R.id.tvName,setNameAge("$nickname,$age"))
                holder.setText(R.id.tvContent,lang)

            }


        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_follwing, parent)
        }


    }
    override fun onDestroy() {
        super.onDestroy()

    }
    override fun clickListener() {
    }

    override fun getViewModel(): Class<FollowViewMode> = FollowViewMode::class.java
}